/** 
 * 2011-10-10 上午11:39:05 
 * Server.java 
 * author:sunb 
 */
package com.u6.udp.test;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author sunbin
 * 
 */
public class Server {

	public static void main(String[] args) {
		try {
			InetSocketAddress laddr = new InetSocketAddress("192.168.0.143", 7188);
			DatagramSocket datagramSocket = new DatagramSocket(laddr);
			ExecutorService executorService = Executors.newCachedThreadPool();
			executorService.execute(new ExecutorTask(datagramSocket));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static class ExecutorTask implements Runnable {
		private DatagramSocket datagramSocket;

		public ExecutorTask(DatagramSocket datagramSocket) {
			this.datagramSocket = datagramSocket;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				byte[] buf = new byte[3200];
				DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
				while (true) {
					datagramSocket.receive(receivePacket);

					String address = receivePacket.getAddress().getHostAddress();
				
					byte[] bs = receivePacket.getData();

					System.out.println(bs.length);

				/*	String sendMessage = "接受到来自：" + address + ":" + receivePacket.getPort() + "的消息";

					bs = sendMessage.getBytes();

					DatagramPacket sendPack = new DatagramPacket(bs, bs.length, receivePacket.getSocketAddress());
					datagramSocket.send(sendPack);*/
				}
			}

			catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
